# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Renato Silva <br.renatosilva@gmail.com>

_realname=cairo
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
_commit='b43e7c6f3cf7855e16170a06d3a9c7234c60ca94'
pkgver=1.17.8
pkgrel=3
pkgdesc="Cairo vector graphics library (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://cairographics.org/"
license=(LGPL2.1 MPL1.1)
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-glib2"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-meson"
             "${MINGW_PACKAGE_PREFIX}-ninja"
            )
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-fontconfig"
         "${MINGW_PACKAGE_PREFIX}-lzo2"
         "${MINGW_PACKAGE_PREFIX}-pixman"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-zlib"
        )
optdepends=("${MINGW_PACKAGE_PREFIX}-glib2: libcairo-gobject")
source=("https://cairographics.org/snapshots/cairo-${pkgver}.tar.xz"
        0026-create-argb-fonts.all.patch
        0030-ucrt-clang-fixes.patch
        https://gitlab.freedesktop.org/cairo/cairo/-/commit/71eb6396c497541118d4c85f97cb9d05aeb94a7e.patch
        https://gitlab.freedesktop.org/cairo/cairo/-/commit/5e42a5277eddafd312a73e355d7775a4401dae4e.patch
        https://gitlab.freedesktop.org/cairo/cairo/-/commit/db4c941c34617c1a1ee0519387b94936491fa860.patch
        https://gitlab.freedesktop.org/cairo/cairo/-/commit/b1bc11f646a5345e61ebd5c58305c536bd6a4207.patch
        0040-revert-dwrite-measuring-default.patch)
sha256sums=('5b10c8892d1b58d70d3f0ba5b47863a061262fa56b9dc7944161f8c8b783bc64'
            '6db6c44fbdb4926d09afa978fe80430186c4b7b7d255059602b1f94c6a079975'
            '86c1af2878a20bd3608fc476e4ba1f2451458a5f6f12e457e37b61082c38db82'
            '3fc6704e75bd93ae6551448e3a9761918069786ff84ecb114831e45e5fd06c35'
            '43a92df715dfdfdcc09294bc8c663bfc1b81c8712ef1f71f350c8b880ec714e8'
            'e9b58e4554d498003c7df9656e50147a30a76b5f102577763a8f79dac78f5236'
            '3fc6704e75bd93ae6551448e3a9761918069786ff84ecb114831e45e5fd06c35'
            '3f61530d96c6f2de5a6dd05db63f2e89edcdc3b061ee54cf66c4d69666405809')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"

  patch -p1 -i ${srcdir}/0026-create-argb-fonts.all.patch
  patch -p1 -i ${srcdir}/0030-ucrt-clang-fixes.patch

  # https://gitlab.freedesktop.org/cairo/cairo/-/merge_requests/441
  patch -p1 -i "${srcdir}/71eb6396c497541118d4c85f97cb9d05aeb94a7e.patch"
  # https://gitlab.freedesktop.org/cairo/cairo/-/issues/634
  patch -p1 -i "${srcdir}/5e42a5277eddafd312a73e355d7775a4401dae4e.patch"
  # https://gitlab.freedesktop.org/cairo/cairo/-/merge_requests/451
  patch -p1 -i "${srcdir}/db4c941c34617c1a1ee0519387b94936491fa860.patch"

  # https://gitlab.freedesktop.org/cairo/cairo/-/issues/637#note_1762681
  patch -R -p1 -i "${srcdir}/b1bc11f646a5345e61ebd5c58305c536bd6a4207.patch"

  # https://gitlab.freedesktop.org/cairo/cairo/-/issues/637#note_1760511
  patch -p1 -i "${srcdir}/0040-revert-dwrite-measuring-default.patch"
}

build() {
  local -a _static_flags=(
    -DGIO_STATIC_COMPILATION
    -DGLIB_STATIC_COMPILATION
    -DGMODULE_STATIC_COMPILATION
    -DGOBJECT_STATIC_COMPILATION
  )

  local -a _meson_options
  _meson_options=(
    --prefix=${MINGW_PREFIX}
    --buildtype=plain
    --wrap-mode=nofallback
    -Dauto_features=enabled
    -Dxlib=disabled
    -Dxcb=disabled
    -Dtests=disabled
    -Dspectre=disabled
    -Dtee=enabled
    -Dxml=enabled
    -Dsymbol-lookup=disabled
  )

  # Enables CAIRO_WIN32_STATIC_BUILD, keeps DllMain far away (#17643)
  CFLAGS+=" ${_static_flags[@]}" \
  CXXFLAGS+=" ${_static_flags[@]}" \
  MSYS2_ARG_CONV_EXCL="--prefix=" \
  ${MINGW_PREFIX}/bin/meson setup \
    "${_meson_options[@]}" \
    --default-library=static \
    "${_realname}-${pkgver}" \
    "build-${MSYSTEM}-static"

  meson compile -C "build-${MSYSTEM}-static"

  MSYS2_ARG_CONV_EXCL="--prefix=" \
  ${MINGW_PREFIX}/bin/meson setup \
    "${_meson_options[@]}" \
    --default-library=shared \
    "${_realname}-${pkgver}" \
    "build-${MSYSTEM}-shared"

  meson compile -C "build-${MSYSTEM}-shared"
}

package() {
  meson install -C "build-${MSYSTEM}-static" --destdir "${pkgdir}"
  meson install -C "build-${MSYSTEM}-shared" --destdir "${pkgdir}"

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING"          "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING-LGPL-2.1" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING-LGPL-2.1"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING-MPL-1.1"  "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING-MPL-1.1"
}
